package com.symria.system.listener;

import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

import com.symria.framework.util.ClassLoaderUtil;
import com.symria.system.servlet.IStartUp;
/**
 * 功能描述: 启动一些系统常用任务
 * @author luo
 * @date Jul 9, 2010
 */
public class Startup extends HttpServlet implements Servlet {
	private static final Logger logger =  Logger.getLogger(Startup.class);
	public void init() throws ServletException {
		startup();
	}


	public void destroy() {
		shutdown();
	}
	public void startup(){
		logger.info("System Start Begin ......");
		Enumeration names = getServletConfig().getInitParameterNames();
		String name = null;
		IStartUp startUp = null;
		while (names.hasMoreElements()) {
			name = (String) names.nextElement();
			logger.info("Load Start  ......[" + name + "]");
			startUp = (IStartUp) ClassLoaderUtil.executeGetIntanceMethod(getServletConfig().getInitParameter(name));
			if (startUp != null) {
				startUp.init();
			}
		}
	}
	
	public void shutdown() {
		logger.info("shutdown startup .....");
		Enumeration names = getServletConfig().getInitParameterNames();
		String name = null;
		IStartUp startUp = null;
		while (names.hasMoreElements()) {
			name = (String) names.nextElement();
			startUp = (IStartUp) ClassLoaderUtil.executeGetIntanceMethod(getServletConfig().getInitParameter(name));
			if (startUp != null) {
				startUp.destory();
			}
		}
	}
}
